//////////////////////////////////////////////////////////////////////////
//
// GAM203
// Text
// Text.cpp
// 
// Grant Johnson
// Qantm College
// 27/10/2010
//
//////////////////////////////////////////////////////////////////////////

#include "Text.h"
#include <stdio.h>

Text::Text()
{
	m_pString = new char[2056];
	ZeroMemory(&m_fontRect, sizeof(m_fontRect));
}
Text::~Text()
{
	//Cleanup
	if(m_pString)
	{
		delete m_pString;
		m_pString = NULL;
	}
	if(m_pFont)
	{
		m_pFont->Release();
		m_pFont = NULL;
	}
}
bool Text::CreateObject(IDirect3DDevice9* a_pDevice, char* a_pTexFileName)
{
	if(!a_pDevice)
	{
		return false;
	}

	return true;
}
bool Text::CreateObject(IDirect3DDevice9* a_pDevice, char* a_pTexFileName, int fontSize, const char* a_pString, int a_x, int a_y, D3DCOLOR a_color)
{
	//Ensure we are given a valid device
	if(!a_pDevice)
	{
		return false;
	}
	//set the font size to the one passed in
	m_fontSize = fontSize;

	//Set the string used by the text
	sprintf_s(m_pString, 512, "%s", a_pString);

	//Create the front rect/position based on passed in information
	m_fontRect.left = (long)a_x;
	m_fontRect.top = (long)a_y;
	m_fontRect.right = (long)a_x+1;
	m_fontRect.bottom = (long)a_y+1;

	//Set the colour to the one passed in
	m_color = a_color;

	//Create the font
	D3DXCreateFont(a_pDevice, m_fontSize, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);

	return true;
}
void Text::Render(float a_dt, IDirect3DDevice9* a_pDevice, const RenderParams& a_renderParams)
{
	//If a valid device, render the text
	if(a_pDevice)
	{
		m_pFont->DrawTextA(NULL, m_pString, -1, &m_fontRect, DT_LEFT | DT_NOCLIP, m_color);
	}
}

void Text::SetText(const char* a_pString)
{
	//Set the text to the string passed in
	sprintf_s(m_pString, 2056, "%s", a_pString);
}
void Text::SetPosition(float a_x, float a_y)
{
	//Set the rect/position
	ZeroMemory(&m_fontRect, sizeof(m_fontRect));
	m_fontRect.left = (long)a_x;
	m_fontRect.top = (long)a_y;
	m_fontRect.right = (long)a_x+1;
	m_fontRect.bottom = (long)a_y+1;
}
void Text::SetColor(D3DCOLOR a_color)
{
	//Set the colour
	m_color = a_color;
}